﻿using DayuCode.Designer.Common;
using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms.template
{
    public partial class FormCodeFragment : UIForm
    {
        long? codeFragmentId;
        CodeFragment codeFragment;
        public FormCodeFragment(long? codeFragmentId)
        {
            InitializeComponent();

            this.codeFragmentId = codeFragmentId;
            if (codeFragmentId != null)
            {
                codeFragment = CodeFragmentHandler.getCodeFragment((long)codeFragmentId);
            }
            else
            {
                codeFragment = new CodeFragment();
            }

            txtName.Text = codeFragment.Name;
            txtCode.Text = codeFragment.Code;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string code = txtCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(name))
            {
                this.ShowErrorTip("名称不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(code))
            {
                this.ShowErrorTip("编码不能为空");
                return;
            }
            codeFragment.Name = name;
            codeFragment.Code = code;
            CodeFragmentHandler.saveCodeFragment(codeFragment);

            Close();
            DialogResult = DialogResult.OK;
        }

        public CodeFragment CodeFragment
        {
            get { return codeFragment; }
        }
    }
}
